package com.zoe.onelink.business.web.arg;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zoe.onelink.annotation.web.arg.RequestArg;
import com.zoe.onelink.business.entity.AdvanceQuery;
import com.zoe.onelink.core.property.OnelinkProperties;
import com.zoe.onelink.core.web.resolver.AbstractObjectMapperMethodArgumentResolver;
import org.springframework.core.MethodParameter;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-20
 */
public class AdvanceQueryArgumentResolver extends AbstractObjectMapperMethodArgumentResolver {

    public AdvanceQueryArgumentResolver(ObjectMapper objectMapper, OnelinkProperties onelinkProperties) {
        super(objectMapper, onelinkProperties.getWeb().getRequestArg());
    }

    @Override
    protected boolean couldResolveArgument(MethodParameter parameter) {
        return AdvanceQuery.class.isAssignableFrom(parameter.getParameterType());
    }

    @Override
    protected Object resolveArgument(JsonNode rootNode, JavaType paramType, RequestArg requestArg, String paramName, int paramIndex) throws Exception {
        JsonNode advanceNode = rootNode.path(paramName);
        if (advanceNode == null || advanceNode.isMissingNode()) {
            return null;
        }
        AdvanceQuery advanceQuery = this.om.readValue(advanceNode.traverse(), AdvanceQuery.class);
        // 默认不再支持自动在上下文中设置AdvanceQuery对象,该方式过于隐蔽,且第一个查询会被强制使用AdvanceQuery,不够灵活,应该引导开发在Dao层显示传入AdvanceQuery对象
        // AdvanceQuery.set(advanceQuery);
        return advanceQuery;
    }
}
